package by.sjc.giz.dao.repositiory.impl;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import by.sjc.giz.dao.base.AbstractHibernateDao;
import by.sjc.giz.dao.entity.AccountEntity;
import by.sjc.giz.dao.entity.BookEntity;
import by.sjc.giz.dao.repositiory.AccountDao;

/**
 * Created by Z on 07.08.14.
 */
@Repository
@Transactional
public class AccountRepository extends AbstractHibernateDao<AccountEntity, Long> implements AccountDao {

    public static final Logger logger = Logger.getLogger(AccountRepository.class);

    @Override
    public AccountEntity loadAccountByCredentials(String email, String password) {
        logger.debug("EMAIL::::::: " + email);

        @SuppressWarnings("JpaQlInspection")
        String hql = "from AccountEntity a where a.email = :email and a.password = md5(:password) ";

        //@SuppressWarnings("unchecked")
        return (AccountEntity) getSession().createQuery(hql)
                .setParameter("email", email)
                .setParameter("password", password).uniqueResult();
    }

    @Override
    public AccountEntity loadAccountByEmail(String email) {
        logger.debug("EMAIL::::::: " + email);

        @SuppressWarnings("JpaQlInspection")
        String hql = "from AccountEntity a where a.email = :email";

        //@SuppressWarnings("unchecked")
        return (AccountEntity) getSession().createQuery(hql)
                .setParameter("email", email).uniqueResult();
    }
    
    @Override
    public AccountEntity loadAccountById(int id){
    	
    	@SuppressWarnings("JpaQlInspection")
    	String hql = "from AccountEntity where id = :id";
    	AccountEntity accEntity = (AccountEntity)getSession().createQuery(hql)
    			.setParameter("id", id).uniqueResult();
    	if( ! Hibernate.isInitialized(accEntity.getFavoriteBookEntitiesSet()) ){
    		Hibernate.initialize(accEntity.getFavoriteBookEntitiesSet());
    	}
    	return accEntity; 
    }

    @Override
    public void registerAccount(AccountEntity accountEntity) {
        try {
            Session session = getSession();
            session.save(accountEntity);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<AccountEntity> getSubjectAccounts(int subjectId) {
        @SuppressWarnings("JpaQlInspection")
        String hql = "from AccountEntity where subject_id=:subjectId";
        return (List<AccountEntity>) getSession().createQuery(hql)
                .setParameter("subjectId",subjectId)
                .list();
    }

	@Override
	public void updateAccount(AccountEntity accountEntity) {
       logger.info("updating =" + accountEntity);
        getSession().saveOrUpdate(accountEntity);
        logger.info(":::::::::::::::::end updating:::::::::::::::");		
	}
}
